import os.path

import requests
from lxml import etree

url = 'https://www.umei.cc/bizhitupian/diannaobizhi/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
}
res = requests.get(url, headers=headers)
html = res.content.decode()
# print(html)
tree = etree.HTML(html)
# 懒加载 真正的图片地址是存储在img标签data-original的属性中
src_list = tree.xpath('//div[@class="item masonry_brick"]//img')
# print(src_list)
if not os.path.exists('img'):
    os.mkdir('img')
for img in src_list:
    # 图片的url
    url = img.xpath('./@data-original')[0]
    # 图片的alt属性 用于图片的名字
    alt = img.xpath('./@alt')[0]
    res = requests.get(url, headers=headers)
    with open('./img/'+alt+'.jpg', 'wb') as f:
        f.write(res.content)
    print(url, alt, '下载完成~')
